package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.DynamicContentMapper;
import cn.wolfcode.trip.base.mapper.FriendMapper;
import cn.wolfcode.trip.base.mapper.FriendPraiseMapper;
import cn.wolfcode.trip.base.mapper.TagMapper;
import cn.wolfcode.trip.base.query.DynamicContentQueryObject;
import cn.wolfcode.trip.base.query.FriendQueryObject;
import cn.wolfcode.trip.base.service.IDynamicContentService;
import cn.wolfcode.trip.base.service.ITagService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class DynamicContentServiceImpl implements IDynamicContentService {
    @Autowired
    private DynamicContentMapper dynamicContentMapper;
    @Autowired
    private FriendMapper friendMapper;
    @Autowired
    private FriendPraiseMapper friendPraiseMapper;

    /**
     * 新增一条朋友圈动态
     * 并往可访问表中添加数据
     * @param dynamicContent
     */
    public void save(DynamicContent dynamicContent,String[] visits) {
        //需要维护动态表和可访问用户中间表的关系
        dynamicContentMapper.insert(dynamicContent);
        if(visits!=null){
            for (String visit : visits) {
                dynamicContentMapper.insertRelation(dynamicContent.getId(),visit);
            }
        }
    }

    /**
     * 按页获取用户能够观看的朋友圈动态
     *
     * @param qo
     * @return
     */
    public PageInfo query(DynamicContentQueryObject qo) {
        HashSet hashSet = new HashSet();
        //获取用户的所有好友数据
        FriendQueryObject friendQo = new FriendQueryObject();
        friendQo.setUserId(qo.getUserId());
        List<Friend> flist = friendMapper.selectForListByUser(friendQo);
        for (Friend friend : flist) {
            hashSet.add(friend.getUser1().getId());
            hashSet.add(friend.getUser2().getId());
        }
        System.out.println(hashSet);
        qo.setFriends(hashSet);
        //翻页查询
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<DynamicContent> list = dynamicContentMapper.selectForList(qo);
        //获取登录者的好友
        User user = UserContext.getCurrentUser();
        FriendQueryObject fqo = new FriendQueryObject();
        fqo.setUserId(qo.getUserId());
        List<Friend> currentFriend = friendMapper.selectForListByUser(fqo);
        HashSet friendSet = new HashSet();
        for (Friend friend : currentFriend) {
            friendSet.add(friend.getUser1().getId());
            friendSet.add(friend.getUser2().getId());
        }
        //比对动态中的点赞人，是否在用户的好友里面，有就显示出来
        for (DynamicContent dynamicContent : list) {
            String temp="";
            int count =0;
            for (FriendPraise fp : dynamicContent.getFriendPraises()) {
                count++;
                if(friendSet.contains(fp.getUser().getId())){
                    temp+=fp.getUser().getNickName()+" ";
                }
            }
            temp+=" 等"+count+"人觉得赞";
            dynamicContent.setFriendPraisesString(count==0?"0人点赞":temp);
        }
        return new PageInfo(list);
    }
}